package com.zds.zhouyi.ui;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.view.Window;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.zds.zhouyi.R;

public class LoadingDialog extends Dialog {

    ObjectAnimator rotateAnimation;
    ImageView ivLoading;

    public LoadingDialog(@NonNull Context context) {
        super(context);
        init();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_loading);
        // 自定义的 loading 布局文件
//        getWindow().setBackgroundDrawableResource(R.color.black3D4550);
        setCancelable(false); // 设置弹窗不可取消
        ivLoading = findViewById(R.id.iv_loading);
    }

    // 显示 loading 弹窗
    public void showLoading() {
        if (!isShowing()) {
            show();
        }

        // 创建旋转动画对象
        rotateAnimation = ObjectAnimator.ofFloat(ivLoading, "rotation", 0f, 360f);
        rotateAnimation.setDuration(500); // 设置动画持续时间，单位为毫秒
        rotateAnimation.setRepeatCount(ObjectAnimator.INFINITE); // 设置重复次数，这里表示无限循环
        // 开始动画
        rotateAnimation.start();

    }

    // 隐藏 loading 弹窗
    public void hideLoading() {
        if (rotateAnimation != null) {
            rotateAnimation.cancel();
        }
        if (isShowing()) {
            dismiss();
        }
    }
}
